package org.yufan.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.yufan.admin.bean.EasyUIResult;
import org.yufan.admin.bean.ItemVo;
import org.yufan.bean.Item;
import org.yufan.bean.ItemDesc;
import org.yufan.common.Result;
import org.yufan.common.ResultUtils;
import org.yufan.exception.CustomerException;
import org.yufan.service.ItemDescService;
import org.yufan.service.ItemService;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemDescService itemDescService;


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Result itemAdd(Item item, String desc){
        itemService.addItem(item,desc);
        return ResultUtils.buildSuccess();
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result updateAdd(Item item,String desc){
        itemService.updateItem(item,desc);
        return ResultUtils.buildSuccess();
    }

    @RequestMapping(value = "/reshelf",method = RequestMethod.POST)
    @ResponseBody
    public Result reshelf(ItemVo itemVo){
        itemService.reshelf(itemVo.getIds());
        return ResultUtils.buildSuccess();
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public EasyUIResult list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "rows",defaultValue = "10") Integer rows){
        //分页查询数据
        PageInfo<Item> pageInfo = itemService.queryPageListByWhere(null, page, rows);
        return new EasyUIResult(pageInfo.getTotal(),pageInfo.getList());
    }


    @RequestMapping(value = "/desc/{itemId}",method = RequestMethod.GET)
    public ResponseEntity<ItemDesc> queryItemDesc(@PathVariable("itemId")Long itemId) throws CustomerException {
       return  ResponseEntity.ok(itemDescService.queryById(itemId));
    }


}
